﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.Web.Course;

public partial class sysadmin_QuestionManager_EditQuestionInfo_duoxuan : PowerCreator.LMS.Web.Base.BasePage
{
    //ManagerCourseInfo courseInfoManager = new ManagerCourseInfo();
    //ManagerQuestionTypeInfo questionTypeManager = new ManagerQuestionTypeInfo();

    //ManagerQuestionInfo questionInfoManager = new ManagerQuestionInfo();
    ManagerQuestion objQuestion = new ManagerQuestion();
    Course objCourse = new Course();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["TypeID"] = Request.QueryString["TypeID"].ToString();
            ViewState["QuestionID"] = Request.QueryString["qid"].ToString();
            ViewState["UserID"] = GetCreatedByInfo().UserID;
            Pageload();
            InitPage();
        }
    }

    protected override void InitPage()
    {
       
        lab_ContentTitle.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_ContentTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionCount.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionOption.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionOption", PowerCreator.LMS.Language.LanagetItemType.Page);
       

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnBack.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_AddOptionC.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddOptionD.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddOptionE.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void bandList(int ItemCount)
    {
        ListItem objlist;
        for (int i = 0; i < ItemCount; i++)
        {
            objlist = new ListItem();
            switch (i)
            {
                case 0: objlist.Text = "A";
                    
                    break;
                case 1: objlist.Text = "B";
                    break;
                case 2: objlist.Text = "C";
                    break;
                case 3: objlist.Text = "D";
                    break;
                case 4: objlist.Text = "E";
                    break;
                case 5: objlist.Text = "F";
                    break;
            }
            objlist.Value = i.ToString();

           
        }
    }

    private void Pageload()
    {


        VOQuestion objQuestionInfo = new VOQuestion();
        objQuestionInfo = objQuestion.LoadQuestion(int.Parse(ViewState["QuestionID"].ToString()));
        ViewState["objQuestionInfo"] = objQuestionInfo;

        DataTable dt_QuestionOption = new DataTable();
        string search = "FKQuestionOptionQuestionID=" + ViewState["QuestionID"].ToString();
        dt_QuestionOption = objQuestion.QueryQuestionOption(search);
        ViewState["dt_QuestionOption"] = dt_QuestionOption;

        bandList(dt_QuestionOption.Rows.Count);

        txtQuestionContent.Text = objQuestionInfo.QuestionContent;



        #region 绑定选项内容



        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
            string Option = Content.Substring(0, 1);
            if (Option == "A")
            {
                txtOptionA.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "B")
            {
                txtOptionB.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "C")
            {
                txtOptionC.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "D")
            {
                txtOptionD.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "E")
            {
                txtOptionE.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else
            {
                txtOptionF.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }

        }
        #endregion

        #region 隐藏未选的选项
        //if (dt_QuestionOption.Rows.Count == 1)
        //{
        //    btn_AddOptionA.Visible = true;
        //    Pnl_B.Visible = false;
        //    Pnl_C.Visible = false;
        //    Pnl_D.Visible = false;

        //    Pnl_E.Visible = false;
        //    Pnl_F.Visible = false;
        //}
        //if (dt_QuestionOption.Rows.Count == 2)
        //{
        //    btn_AddOptionA.Visible = false;
        //    Pnl_B.Visible = true;

        //    Pnl_C.Visible = false;
        //    Pnl_D.Visible = false;

        //    Pnl_E.Visible = false;
        //    Pnl_F.Visible = false;
        //}
        if (dt_QuestionOption.Rows.Count == 3)
        {
            //btn_AddOptionA.Visible = false;

            //Pnl_B.Visible = true;
            //btn_AddOptionB.Visible = false;

            btn_AddOptionC.Visible = true;
            Pnl_D.Visible = false;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 4)
        {
            //btn_AddOptionA.Visible = false;

            //Pnl_B.Visible = true;
            //btn_AddOptionB.Visible = false;

            //Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 5)
        {
            //btn_AddOptionA.Visible = false;

            //Pnl_B.Visible = true;
            //btn_AddOptionB.Visible = false;

            //Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;
            btn_AddOptionD.Visible = false;

            Pnl_E.Visible = true;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 6)
        {
            //btn_AddOptionA.Visible = false;

            //Pnl_B.Visible = true;
            //btn_AddOptionB.Visible = false;

            //Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;
            btn_AddOptionD.Visible = false;

            Pnl_E.Visible = true;
            btn_AddOptionE.Visible = false;
            Pnl_F.Visible = true;
        }
        #endregion
    }

    private bool PageCheck()
    {
        if (txtOptionA.Text.Trim() == "" | txtOptionB.Text.Trim() == "" | txtOptionC.Text.Trim() == "")
        {
            Script.Alert(this, "msg", GetLanageValueByKey("AddPaper_msg14", PowerCreator.LMS.Language.LanagetItemType.Page) + "A、B、C" + GetLanageValueByKey("AddPaper_msg15", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        return true;
    }
    private string ConvertOptionID(int Index)
    {
        string OptionID = string.Empty;
        switch (Index)
        {
            case 0: OptionID = "A";
                break;
            case 1: OptionID = "B";
                break;
            case 2: OptionID = "C";
                break;
            case 3: OptionID = "D";
                break;
            case 4: OptionID = "E";
                break;
            case 5: OptionID = "F";
                break;
        }
        return OptionID;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!PageCheck())
        {
            return;
        }
        StringBuilder str = new StringBuilder();
        ArrayList list = new ArrayList();

        ArrayList Optionlist = new ArrayList();
        Optionlist.Add(txtOptionA.Text.Trim());
        Optionlist.Add(txtOptionB.Text.Trim());
        Optionlist.Add(txtOptionC.Text.Trim());
        Optionlist.Add(txtOptionD.Text.Trim());
        Optionlist.Add(txtOptionE.Text.Trim());
        Optionlist.Add(txtOptionF.Text.Trim());

        int maxindex = 0;

        for (int j = 0; j < maxindex + 1; j++)
        {
            string OptionID_j = ConvertOptionID(j);

            if (Optionlist[j].ToString() == "")
            {
                Script.Alert(Page, "msg", GetLanageValueByKey("AddPaper_msg14", PowerCreator.LMS.Language.LanagetItemType.Page) +  OptionID_j  + GetLanageValueByKey("AddPaper_msg15", PowerCreator.LMS.Language.LanagetItemType.Page));
                return;
            }
        }
        for (int i = 0; i < Optionlist.Count; i++)
        {
            string OptionID = ConvertOptionID(i);
            if (Optionlist[i].ToString() != "")
            {
                list.Add(OptionID + "、" + Optionlist[i].ToString());
            }
        }

        try
        {
            #region 删除选项


            DataTable dt_QuestionOption = (DataTable)ViewState["dt_QuestionOption"];
            for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
            {
                string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
                string Option = Content.Substring(0, 1);
                string optionID = dt_QuestionOption.Rows[i]["QuestionOptionID"].ToString();
                objQuestion.DeleteQuestionOption(int.Parse(optionID));

            }
            #endregion

            VOQuestion objQuestionInfo = new VOQuestion();
            objQuestionInfo = (VOQuestion)ViewState["objQuestionInfo"];
            objQuestionInfo.FKQuestionCourseID =-1;
            objQuestionInfo.QuestionCreateID = int.Parse(ViewState["UserID"].ToString());
            //info.DateCreated = DateTime.Now;



            objQuestionInfo.QuestionAnswer = str.ToString();
            objQuestionInfo.QuestionDefaultScore = 0;
            objQuestionInfo.QuestionDiffculty =0;
            objQuestionInfo.QuestionKen = "";


            objQuestionInfo.QuestionContent = txtQuestionContent.Text;


            objQuestionInfo.QuestionState = 2;         //禁用1,启用2

            VOQuestionOption objQuestionOptionInfo = new VOQuestionOption();

            for (int i = 0; i < list.Count; i++)
            {
                objQuestionOptionInfo.FKQuestionOptionQuestionID = objQuestionInfo.QuestionID;
                objQuestionOptionInfo.QuestionOptionContent = list[i].ToString();
                objQuestionOptionInfo.QuestionOptionCreateID = int.Parse(ViewState["UserID"].ToString());
                objQuestionOptionInfo.QuestionOptionCreateTime = DateTime.Now;
                objQuestion.CreateQuestionOption(objQuestionOptionInfo);
            }



            if (objQuestion.UpdateQuetion(objQuestionInfo))//更新试题信息
            {
                Script.Alert(GetLanageValueByKey("msg_CelueSelectQuest_shuruti23", PowerCreator.LMS.Language.LanagetItemType.Page));
            }
            else
            {
                Script.Alert(GetLanageValueByKey("msg_CelueSelectQuest_shuruti24", PowerCreator.LMS.Language.LanagetItemType.Page));
            }
        }
        catch
        {    
            Script.Alert(GetLanageValueByKey("msg_CelueSelectQuest_shuruti24", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        Script.RedirectURL("ManagerQuestionInfo.aspx");
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("ManagerQuestionInfo.aspx");
    }
    protected void btn_AddOptionC_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "D";
        objlist.Value = "3";
        //chbox_answerlist.Items.Add(objlist);
        Pnl_D.Visible = true;
        btn_AddOptionC.Visible = false;
    }
    protected void btn_AddOptionD_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "E";
        objlist.Value = "4";
        //chbox_answerlist.Items.Add(objlist);
        Pnl_E.Visible = true;
        btn_AddOptionD.Visible = false;
    }
    protected void btn_AddOptionE_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "F";
        objlist.Value = "5";
        //chbox_answerlist.Items.Add(objlist);
        Pnl_F.Visible = true;
        btn_AddOptionE.Visible = false;
    }
}
